package de.herberlin.server.common.event;

import java.util.LinkedList;

import javax.swing.table.AbstractTableModel;

import de.herberlin.server.common.ConfigConstants;
import de.herberlin.server.common.Configuration;

/**
 *
 * @author Hans Joachim Herbertz
 * @created 08.02.2003
 */
public class LogTableModel extends AbstractTableModel {

	private LinkedList dataList = new LinkedList();

	private Object[] columnNames=new String[] { "Time",
			"Request", "Response", "Content" };

	/**
	 * Constructor for LogTableModel.
	 */
	public LogTableModel() {
		super();
	}

	public String getColumnName(int column) {
		if (columnNames!=null && columnNames.length>column) {
			return (String)columnNames[column];
		} else {
			return "";
		}
	}

	public void add(BasicEvent event) {

		if (event instanceof ServerStartEvent) {

			this.columnNames=((ServerStartEvent)event).getColumnNames();
			dataList=new LinkedList();
			fireTableStructureChanged();



		} else if (event instanceof RequestEvent){

			// display the event

			if (event.getValueAt(3) != null) {
				if (Configuration.getPrefs().getBoolean(
						ConfigConstants.DISPLAY_TEXTONLY, false)
						&& !event.getValueAt(3).toString().startsWith("text")) {
					return;
				}
			}
			dataList.addFirst(event);
			if (dataList.size() > Configuration.getPrefs().getInt(
					ConfigConstants.LOGSIZE, 100)) {
				dataList.removeLast();
			}
			fireTableRowsInserted(0, 1);
		}
	}

	/**
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	public int getRowCount() {
		return dataList.size();
	}

	/**
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	public int getColumnCount() {
		return 4;
	}

	/**
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= dataList.size())
			return null;
		if (columnIndex >= 4)
			return null;
		BasicEvent event = (BasicEvent) dataList.get(rowIndex);
		return event.getValueAt(columnIndex);
	}
}
